package main

import (
	"fmt"
	"io/ioutil"
)

func tryif() {
	const filename = "abc.txt"
	if contents, err := ioutil.ReadFile(filename); err != nil {
		fmt.Println(err)
	} else {
		fmt.Printf("%s\n", contents)
	}
	// fmt.Print(contents) // 此处无法打印contents因为 contents是在if的作用域里定义的除了这个作用域contents是不存在
}

// 测试读取一个文本文件
func main() {
	fmt.Println(grade(68))
}

// switch 不需要加break，程序会自动break、除非使用 fallthrough
func eval(a, b int, op string) int {
	var result int
	switch op {
	case "+":
		result = a + b
	case "-":
		result = a - b
	case "*":
		result = a * b
	case "/":
		result = a / b
	default:
		panic("unsupported operator:" + op)
	}
	return result
}

// switch 后面没有表达式，可以在case中判断条件
func grade(score int) string {
	g := ""
	switch {
	case score < 0 || score > 100:
		panic(fmt.Sprintf("Wrong score: %d", score))
	case score < 60:
		g = "F"
	case score < 80:
		g = "C"
	case score < 90:
		g = "B"
	case score <= 100:
		g = "A"
	}
	return g
}
